<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handlePrint({})" v-auth="'rd:apply:matMoreApply:edit'">
          <Icon icon="simple-line-icons:printer" /> {{ t('打印') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'rd:apply:matMoreApply:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <PrintModal @register="registerModal" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsM8RdApplyMatMoreApplyList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    matMoreApplyDelete,
    matMoreApplyListData,
    matMoreApplyDisable,
    matMoreApplyEnable,
  } from '/@/api/m8/rd/apply/matMoreApply';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('rd.apply.matMoreApply');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('补料申请管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('单据编号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('图名'),
        field: 'orderPic.picname',
        component: 'Input',
      },
      {
        label: t('补料说明'),
        field: 'remarks',
        component: 'Input',
      },
      {
        label: t('申请人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('停止补料'),
        field: 'bclose',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据编号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('图名'),
      dataIndex: 'orderPic.picname',
      key: 'a.pic_id',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('材料'),
      dataIndex: 'orderPic.cmat',
      width: 100,
      align: 'left',
    },
    {
      title: t('规格'),
      dataIndex: 'xlSize',
      width: 100,
      align: 'left',
    },
    {
      title: t('补料类型'),
      dataIndex: 'ctype',
      width: 150,
      align: 'left',
      dictType: 'm8_more_type',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'center',
    },
   
    {
      title: t('补料说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('采购数'),
      dataIndex: 'sumPuQty',
      key: 'a.sum_pu_qty',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('入库数'),
      dataIndex: 'sumPuInQty',
      key: 'a.sum_pu_in_qty',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('停止补料'),
      dataIndex: 'bclose',
      key: 'a.bclose',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('申请人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    // {
    //   title: t('修改人'),
    //   dataIndex: 'updateByName',
    //   key: 'a.update_by_name',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    // },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑补料申请'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'rd:apply:matMoreApply:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用补料申请'),
        popConfirm: {
          title: t('是否确认停用补料申请'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'rd:apply:matMoreApply:edit',
        ifShow: () => record.status === '0' && record.bclose == '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用补料申请'),
        popConfirm: {
          title: t('是否确认启用补料申请'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'rd:apply:matMoreApply:edit',
        ifShow: () => record.status === '0' && record.bclose == '1',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除补料申请'),
        popConfirm: {
          title: t('是否确认删除补料申请'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'rd:apply:matMoreApply:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'i-fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload,getSelectRows }] = useTable({
    api: matMoreApplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
        type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const params = { code: record.code };
    const res = await matMoreApplyDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { code: record.code };
    const res = await matMoreApplyEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await matMoreApplyDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Flow_Mat_apply', bizKey: record.id });
  }


  const [registerModal, { openModal }] = useModal();
  // 打印
  const handlePrint = () => {
    let rows = getSelectRows();
    if (rows.length <= 0) {
      showMessage('请至少选择一条要打印的数据!');
    } else {
      let ids = '';
      rows.map((item) => {
        if (ids == '') {
          ids = item.id;
        } else {
          ids = ids + ',' + item.id;
        }
      });
      let obj = {
        title: '补料单打印',
        ids: ids,
        height: '0.9',
        width: '0.9',
        fileName: 'printMoreApply',
      };
      openModal(true, obj);
    }
  };
</script>
